import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_app/models/room_list_item_data.dart';
import 'package:flutter_app/pages/search/search_item.dart';
import 'package:flutter_app/scoped_model/auth.dart';
import 'package:flutter_app/utils/dio_http.dart';
import 'package:flutter_app/utils/scoped_model_helper.dart';

class MyCollectionPage extends StatefulWidget {
  const MyCollectionPage({Key? key}) : super(key: key);

  @override
  _MyCollectionPageState createState() => _MyCollectionPageState();
}

class _MyCollectionPageState extends State<MyCollectionPage> {
  List<RoomListItemData> collectList = [];
  _getData() async {
    var auth = ScopedHelperModel.getModel<AuthModel>(context);
    var token = auth.token;
    var res = await DioHttp.of(context).get('/user/favorites', null, token);
    var resMap = json.decode(res.toString());
    List collectionData = resMap["body"];
    List<RoomListItemData> resList =
        collectionData.map((e) => RoomListItemData.fromJson(e)).toList();
    setState(() {
      collectList = resList;
    });
  }

  // @override
  // void initState() {
  //   _getData();
  //   super.initState();
  // }
  @override
  void didChangeDependencies() {
    _getData();
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('我的收藏'),
      ),
      body: ListView(
        children: collectList.map((e) => HouseItem(e)).toList(),
      ),
    );
  }
}
